package com.zhentao.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zhentao.mapper.CdzUserIncomeMapper;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.common.pojo.income.CdzUserIncome;
import com.zhentao.service.ICdzUserIncomeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.userLocal.UserThreadLocalUtil;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author mp
 * @since 2024-12-25
 */
@Service
public class CdzUserIncomeServiceImpl extends ServiceImpl<CdzUserIncomeMapper, CdzUserIncome> implements ICdzUserIncomeService {

    @Override
    public ResponseResult select() {
        //获取用户id
        Long userId = UserThreadLocalUtil.getUser();
        //根据登录人的id进行查询登录人的收益
        LambdaQueryWrapper<CdzUserIncome> queryWrapper = Wrappers.<CdzUserIncome>lambdaQuery()
                .eq(CdzUserIncome::getUserId, userId)
                //根据创建时间倒叙
                .orderByDesc(CdzUserIncome::getCreateTime);
        //遍历集合
        List<CdzUserIncome> userIncomeList = this.list(queryWrapper);
        for (CdzUserIncome userIncome : userIncomeList){
            userIncome.setInComes("+"+userIncome.getIncome() + "元");
        }
        if (this.list(queryWrapper).size() == 0) {
            return ResponseResult.errorResult(500, "暂无数据");
        }
        return ResponseResult.okResult(userIncomeList);
    }

    @Override
    public ResponseResult selectCount() {
        //获取用户id
        Long userId = UserThreadLocalUtil.getUser();
        //根据登录人的id进行查询登录人的收益
        LambdaQueryWrapper<CdzUserIncome> queryWrapper = Wrappers.<CdzUserIncome>lambdaQuery()
                .eq(CdzUserIncome::getUserId, userId)
                //根据创建时间倒叙
                .orderByDesc(CdzUserIncome::getCreateTime);
        // 定义一个常量
        double count = 0;
        //遍历集合
        List<CdzUserIncome> userIncomeList = this.list(queryWrapper);
        for (CdzUserIncome userIncome : userIncomeList){
            count= count+userIncome.getIncome();
        }
        return ResponseResult.okResult(count);
    }
}
